package com.jwtter.generator;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.jwtter.generator.core.Column;
import com.jwtter.generator.db.DBConnectionUtil;
import com.jwtter.generator.db.sql.AllTabColumns;
import com.jwtter.generator.db.sql.UserColComments;
import com.jwtter.generator.db.sql.UserTabComment;
import com.jwtter.generator.utils.CamelNameUtil;
import com.jwtter.generator.utils.DateUtils;
import com.jwtter.generator.utils.FileAppendUtil;
import com.jwtter.generator.utils.FileUtils;
import com.jwtter.generator.utils.Keyword;
import com.jwtter.generator.utils.PropertiesUtils;
import com.jwtter.generator.utils.Symbols;

/**
 * Java 实体生成
 * 
 * @date 2017年6月15日
 * @author jinwt
 * 
 *
 */
public class JavaModelGenerator extends JavaFileGenerator {
	private static Connection connection = null;
	private static List<UserTabComment> comments = null;
	private static List<UserColComments> colComments = null;
	private static List<AllTabColumns> allTabColumns = null;

	static {
		connection = DBConnectionUtil.getConnection(null);
		comments = TableUtils.getUserTabComments(connection);
		colComments = TableUtils.getUserColComments(connection, "CP_QUOTE");
		allTabColumns = TableUtils.getAllTabColumns(connection, "CP_QUOTE");

	}

	public static void main(String[] args) {

		testTables();
		// for (int i = 0; i < allTabColumns.size(); i++) {
		// AllTabColumns columns = allTabColumns.get(i);
		// String datatype = getDataType(columns.getDATA_TYPE(),
		// String.valueOf(columns.getDATA_PRECISION()), String.valueOf(columns.getDATA_SCALE()));
		// getJavaType(datatype);
		// System.out.println(addImports(datatype));
		// }
	}
	public static void testTables() {
		// List<Table> tables = DBConnectionUtil.getTables(connection.getMetaData());

		javaFile(comments.get(0));
		javaFile(comments);

	}
	public static void javaFile(UserTabComment comment) {
		String package1 = Keyword.PACKAGE + "packageName;";
		System.out.println(package1);
		allTabColumns = TableUtils.getAllTabColumns(connection, "CP_QUOTE");
		Set<String> imports = new HashSet<>();
//		addImports(null);

		System.out.println();
		System.out.println("/**");
		System.out.println("* " + comment.getComments());
		System.out.println("* @date " + DateUtils.getFormatDate(new Date(), null));
		System.out.println("* @author " + "generator");
		System.out.println("*");
		System.out.println("*/");
		System.out.println();
		System.out.println();
		String javaClass = Keyword.PUBLIC + Keyword.CLASS + CamelNameUtil.camelName(comment.getTableName(), false) + "{";
		System.out.println(javaClass);
		List<Column> columns = getColumns(colComments, allTabColumns);
		addFields(null, columns);
		addGetterAndSetter(null, columns);
		System.out.println("}");
		//
	}
	/**
	 * 创建文件
	 * 
	 * @param comments
	 */
	public static void javaFile(List<UserTabComment> comments) {
		UserTabComment comment = comments.get(0);
		String path = PropertiesUtils.getString("targetPackage") + PropertiesUtils.getString("package").replace(".", "/");
		String fileName = path + "/" + CamelNameUtil.camelName(comment.getTableName(), false) + ".java";
		String package1 = Keyword.PACKAGE + PropertiesUtils.getString("package") + ";";
		FileUtils.createFile(fileName);
		createJavaFile(comment, fileName, package1);

	}
	/**
	 * 添加文件 package import 文件注释 class名
	 * 
	 * @param comment
	 * @param fileName
	 * @param package1
	 */
	private static void createJavaFile(UserTabComment comment, String fileName, String package1) {
		FileAppendUtil.writeLine(fileName, package1);
		allTabColumns = TableUtils.getAllTabColumns(connection, "CP_QUOTE");
		addImports(fileName);
		FileAppendUtil.writeLine(fileName, "");
		String javaClass = Keyword.PUBLIC + Keyword.CLASS + CamelNameUtil.camelName(comment.getTableName(), false) + " {";
		FileAppendUtil.writeLine(fileName, "/**");
		addClassComment(comment, fileName);
		FileAppendUtil.writeLine(fileName, "");
		FileAppendUtil.writeLine(fileName, javaClass);
		FileAppendUtil.writeLine(fileName, "");
		List<Column> columns = getColumns(colComments, allTabColumns);
		addFields(fileName, columns);
		addGetterAndSetter(fileName, columns);
		FileAppendUtil.writeLine(fileName, "}");
		System.out.println("======>" + fileName + " is OK!");
	}
	private static void addImports(String fileName ) {
		Set<String> imports = new HashSet<>();
		for (int i = 0; i < allTabColumns.size(); i++) {
			String datatype = getDataType(allTabColumns.get(i).getDATA_TYPE(), String.valueOf(allTabColumns.get(i).getDATA_PRECISION()), String.valueOf(allTabColumns.get(i).getDATA_SCALE()));
			if (isAddImports(datatype))
				imports.add(Keyword.IMPORT + datatype + ";");
		}
		for (Iterator<String> iterator = imports.iterator(); iterator.hasNext();) {
			String string = (String) iterator.next();
			FileAppendUtil.writeLine(fileName, string);
		}
	}
	private static void addClassComment(UserTabComment comment, String fileName) {
		if (comment.getComments() != null) {
			FileAppendUtil.writeLine(fileName, "* " + comment.getComments());
		}
		FileAppendUtil.writeLine(fileName, " * @date " + DateUtils.getFormatDate(new Date(), null));
		FileAppendUtil.writeLine(fileName, " * @author " + "generator");
		FileAppendUtil.writeLine(fileName, " *");
		FileAppendUtil.writeLine(fileName, " */");
	}
	public static List<Column> getColumns(List<UserColComments> colComments, List<AllTabColumns> allTabColumns) {
		List<Column> colomns = new ArrayList<>();
		for (UserColComments userColComments : colComments) {
			Column column = new Column();
			String columnName = userColComments.getColumnName();
			/** 需要类型转换 */
			for (AllTabColumns allTabColumn : allTabColumns) {
				if (columnName.equals(allTabColumn.getCOLUMN_NAME())) {
					column.setColumnName(columnName);
					column.setComment(userColComments.getComments());
					column.setDataType(String.valueOf(allTabColumn.getDATA_TYPE()));
					column.setPrecision(String.valueOf(allTabColumn.getDATA_PRECISION()));
					column.setScale(String.valueOf(allTabColumn.getDATA_SCALE()));
				}
			}
			colomns.add(column);
		}
		return colomns;

	}

	public static void addFields(String fileName, List<Column> columns) {
		for (Column column : columns) {
			addField(fileName, column);
		}
	}

	public static void addField(String fileName, Column column) {
		String comments = column.getComment();
		String columnName = column.getColumnName();
		System.out.println(Symbols.TAB + "/** " + comments + " */");
		String dataType = getDataType(column.getDataType(), column.getPrecision(), column.getScale());
		String type = getJavaType(dataType);
		System.out.println(Symbols.TAB + Keyword.PRIVATE + type + Symbols.BLANK + CamelNameUtil.camelName(columnName, true) + ";");
		if (fileName != null) {
			if (comments != null) {
				FileAppendUtil.writeLine(fileName, Symbols.TAB + "/** " + comments + " */");
			}
			FileAppendUtil.writeLine(fileName, Symbols.TAB + Keyword.PRIVATE + type + Symbols.BLANK + CamelNameUtil.camelName(columnName, true) + ";");
		}
	}
	public static void addGetterAndSetter(String fileName, List<Column> columns) {
		for (Column column : columns) {
			getter(fileName, column);
			setter(fileName, column);
		}

	}
	private static void setter(String fileName, Column column) {
		StringBuffer buffer = new StringBuffer();
		String columnName = column.getColumnName();
		String dataType = getDataType(column.getDataType(), column.getPrecision(), column.getScale());
		String type = getJavaType(dataType);
		buffer.append(Symbols.TAB + Keyword.PUBLIC + Keyword.VOID + getSetterName(columnName) + "(" + type + " " + CamelNameUtil.camelName(columnName, true) + ") {\n")
				.append(Symbols.TAB + Symbols.TAB + Keyword.THIS + Symbols.DOT + getFieldName(columnName) + Symbols.EQUAL + getFieldName(columnName) + ";\n").append(Symbols.TAB + "}");
		System.out.println(buffer.toString());
		if (fileName != null) {
			FileAppendUtil.writeLine(fileName, buffer.toString());
		}
	}

	private static String getSetterName(String columnName) {
		return "set" + CamelNameUtil.camelName(columnName, false);
	}
	private static String getGetterName(String columnName) {
		return "get" + CamelNameUtil.camelName(columnName, false);
	}
	private static String getFieldName(String columnName) {
		return CamelNameUtil.camelName(columnName, true);
	}

	private static void getter(String fileName, Column column) {
		StringBuffer buffer = new StringBuffer();
		String columnName = column.getColumnName();
		String dataType = getDataType(column.getDataType(), column.getPrecision(), column.getScale());
		String type = getJavaType(dataType);
		buffer.append(Symbols.TAB + Keyword.PUBLIC + type + Symbols.BLANK + getGetterName(columnName) + Symbols.PARENTHESES + Symbols.BLANK + Symbols.OPEN_BRACE + "\n")
				.append(Symbols.TAB + Symbols.TAB + Keyword.RETURN + getFieldName(columnName) + Symbols.SEMICOLON + "\n").append(Symbols.TAB + Symbols.CLOSE_BRACE);
		System.out.println(buffer.toString());
		if (fileName != null) {
			FileAppendUtil.writeLine(fileName, buffer.toString());
		}
	}

	// template
}
